package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Brand;
import com.cskaoyan.service.BrandService;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.ErrState;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jcy
 * 品牌制造商
 */
@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    @RequestMapping("list")
    public BaseRespVo list(Integer id, String name, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        BasePageData data = brandService.list(id, name, param);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Brand brand) {
        int code = brandService.create(brand);
        if (code == ErrState.PARAM_ERROR.getErrno()) {
            return BaseRespVo.fail(ErrState.PARAM_ERROR.getErrno(), ErrState.PARAM_ERROR.getErrmsg());
        }
        if (code == Constant.FAIL) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Brand brand) {
        int code = brandService.delete(brand);
        if (code == Constant.FAIL) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Brand brand) {
        int code = brandService.update(brand);
        if (code == Constant.FAIL) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }
}
